package com.kaigejava.command.command.impl;

import com.kaigejava.command.command.ICommandInterface;
import com.kaigejava.command.oo.Control;

import java.util.Stack;

/**
 * Created by kaigejava on 2019/8/30.
 * 遥控器对象
 */
public class CommandModeControl implements Control {
    //遥控器上开
    private ICommandInterface[] onCommands;
    private ICommandInterface[] offCommands;
    private Stack<ICommandInterface> stack=new Stack<ICommandInterface>();
    public CommandModeControl()
    {
        onCommands=new ICommandInterface[5];
        offCommands=new ICommandInterface[5];

        ICommandInterface noCommand=new NoCommand();

        for(int i=0,len=onCommands.length;i<len;i++)
        {
            onCommands[i]=noCommand;
            offCommands[i]=noCommand;
        }

    }

    public void setCommand(int slot,ICommandInterface onCommand,ICommandInterface offCommand)
    {
        onCommands[slot]=onCommand;
        offCommands[slot]=offCommand;

    }

    @Override
    public void onButton(int slot) {

        onCommands[slot].execute();
        stack.push(onCommands[slot]);
    }

    @Override
    public void offButton(int slot) {

        offCommands[slot].execute();
        stack.push(offCommands[slot]);
    }

    @Override
    public void undoButton() {
        stack.pop().undo();
    }
}
